package com.fannuo.demo.Util.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

public class HbaseClient {
    private Connection connection;
    private String hbaseSiteFilePath = "hbase-site.xml";
    public HbaseClient() {
        connection = toConnect();
    }
    public HbaseClient(String hbaseSiteFilePath) {
        connection = toConnect();
        this.hbaseSiteFilePath = hbaseSiteFilePath;
    }
    private Configuration getConf(String hbaseSiteFilePath) throws IOException {
        Configuration conf = new Configuration();
        conf.addResource(hbaseSiteFilePath);
        return conf;
    }
    private Connection toConnect(){
        Connection conn=null;
        try {
            conn= ConnectionFactory.createConnection(getConf(this.hbaseSiteFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public Connection getConnection() {
        return connection;
    }

    public void close() throws IOException {
        if(connection != null) {
            connection.close();
        }
    }
}
